package de.fhaugsburg.games.boardgames.scrabble;

import de.fhaugsburg.games.boardgames.Board;
import de.fhaugsburg.games.boardgames.Player;
import de.fhaugsburg.games.boardgames.PlayerMode;
import de.fhaugsburg.games.boardgames.exceptions.GameException;
import de.fhaugsburg.games.boardgames.scrabble.exceptions.NoEmptySquareException;
/**
 * The scrabbleengine manage putting of a letter, the calculation of the points, the player order, etc.
 * @author r4smus
 *
 */
public class ScrabbleEngine {
	
	private Player player1;
	private Player player2;
	private ScrabbleBoard board;
	private Player currentPlayer;
	private boolean commited = false;
	private int currentPoints = 0;
	private int wordMultiplier = 1;
	private PlayerMode playerMode = PlayerMode.MULTI;
	
	
	public ScrabbleEngine() {
		board = new ScrabbleBoard();
				
	}
/**
 * put a  piece on an specific empty square	and add the points to the attribute currentPoints.
 * @param piece
 * @param row
 * @param column
 * @throws GameException
 */
	public void setPiece(LetterPiece piece, int row, int column) throws GameException{
		if( ! (board.isEmpty(row, column)) )			
		    throw new NoEmptySquareException(row, column);
		board.putPiece(piece, row, column);
		currentPoints += calcPoints(piece, 	row, column);
		
					
	}
/**	
 * switch between the two possible players
 */
	public void nextPlayer(){
		if(currentPlayer.equals(player1))
			currentPlayer = player2;
		else
			currentPlayer = player1;
	}
/**
 * 
 * multiply the points for a letter with its letterMultiplier and if it is on a wordmultiplier square set the wordmultiplier attribute.
 * @param piece
 * @param row
 * @param column
 * @return letterPoints * letterMultiplier
 */
	private int calcPoints(LetterPiece piece, int row, int column){
		int letterPoints = piece.getPoints();
		int letterMultiplier = 1;
		int points;
		    letterMultiplier = board.getDetails(row, column).getLetterMultiplier();
		    if(wordMultiplier < board.getDetails(row, column).getWordMultiplier())
			wordMultiplier = board.getDetails(row, column).getWordMultiplier();
		return points = letterPoints * letterMultiplier;	
	}

	public Player getPlayer1() {
		return player1;
	}

	public void setPlayer1(Player player1) {
		this.player1 = player1;
		currentPlayer = this.player1;
	}

	public Player getPlayer2() {
		return player2;
	}

	public void setPlayer2(Player player2) {
		this.player2 = player2;
	}
/**
 * calculates the total points of a word and add it to the player, after it the board will be reset and the commited attribute will be set.
 * @param commited
 */
	public void setCommited(boolean commited) {
		int totalPoints = currentPoints * wordMultiplier;
		currentPlayer.addPoints(totalPoints);
		reset();
		this.commited = commited;
	}

	public boolean isCommited() {
		
		return commited;
	}
	public Player getCurrentPlayer() {
		return currentPlayer;
	}

	public void setCurrentPlayer(Player currentPlayer) {
		this.currentPlayer = currentPlayer;
	}
	public ScrabbleBoard getBoard() {
		return board;
	}
/**
 * sets the playerMode in order of a String.
 * @param mode
 */
	public void setPlayerMode(String mode) {
		if(mode.equals("MP"))
		    this.playerMode = PlayerMode.MULTI;
		if(mode.equals("SP"))
			this.playerMode = PlayerMode.SINGLE;
	}

	public PlayerMode getPlayerMode() {
		return playerMode;
	}
/**
 * reset the engine.
 */
	public void reset(){
		wordMultiplier = 1;
		currentPoints = 0;
		
	}


}
